package es.cazadoresmadridejos.beans.dao.photo.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import es.cazadoresmadridejos.beans.dao.photo.PhotoDao;
import es.cazadoresmadridejos.common.bean.dao.impl.DaoImpl;
import es.cazadoresmadridejos.model.Photo;
import es.cazadoresmadridejos.model.Photo.PhotoType;
import es.cazadoresmadridejos.objs.PhotoContainer;
import es.cazadoresmadridejos.objs.web.Page;

@SuppressWarnings("unchecked")
@Repository("photoDao")
public class PhotoDaoImpl extends DaoImpl<Photo> implements PhotoDao{

	@Override
	public Photo newInstance() {
		return new Photo();
	}

	@Override
	public List<Photo> findAllPaginated(Page page, final PhotoType type) {
		return (List<Photo>) getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				StringBuilder hql = new StringBuilder();
				hql
					.append("select p ")
					.append(" from Photo p ")
					.append(" where p.type = :type");

				Query query = session.createQuery(hql.toString());
				
				query.setParameter("type", type);
				
				return query.list();
			}
		});
	}
	
	@Override
	public List<Photo> findAllPaginated(Page page, final PhotoType type, final Long partnerId) {
		return (List<Photo>) getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				StringBuilder hql = new StringBuilder();
				hql
				.append("select p ")
				.append(" from Photo p ")
				.append(" where p.type = :type")
				.append(" and p.partner.id = :partnerId");
				
				Query query = session.createQuery(hql.toString());
				
				query.setParameter("type", type);
				query.setLong("partnerId",partnerId);
				
				return query.list();
			}
		});
	}

	@Override
	public Long countAll(final PhotoType type) {
		return (Long) getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				StringBuilder hql = new StringBuilder();
				hql
					.append("select count(p) ")
					.append(" from Photo p ")
					.append(" where p.type = :type").append("");

				Query query = session.createQuery(hql.toString());
				
				query.setParameter("type", type);
				
				return query.uniqueResult();
			}
		});
	}
	@Override
	public Long countAll(final PhotoType type, final Long partnerId) {
		return (Long) getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				StringBuilder hql = new StringBuilder();
				hql
				.append("select count(p) ")
				.append(" from Photo p ")
				.append(" where p.type = :type")
				.append("	and p.partner.id = :partnerId");
				
				Query query = session.createQuery(hql.toString());
				
				query.setParameter("type", type);
				query.setLong("partnerId",partnerId);			
				
				return query.uniqueResult();
			}
		});
	}

	@Override
	public PhotoContainer getPhoto(final Integer page, final PhotoType type) {
		return (PhotoContainer) getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				StringBuilder hql = new StringBuilder();
				hql
					.append("select p ")
					.append(" from Photo p ")
					.append(" where p.type = :type ")
					.append(" ")
					.append(" order by p.id desc ");

				Query query = session.createQuery(hql.toString());
				
				query.setMaxResults(page + 1);
				
				query.setParameter("type", type);
				
				List<Photo> photos = query.list();
				PhotoContainer container = new PhotoContainer();
				container.setPage(page);
				
				container.setPhoto(photos.get(page));
				
//				
//				if(photos.size() >= 3){
//					if(page == 1){
//						container.setNext(photos.get(1));
//						container.setPhoto(photos.get(0));
//						container.setPrev(null);
//					}else
//					if(page == photos.size()){
//						container.setNext(null);
//						container.setPhoto(photos.get(0));
//						container.setPrev(photos.get(1));
//					}else{
//						container.setNext(photos.get(photos.size() - 2));
//						container.setPhoto(photos.get(photos.size() - 3));
//						container.setPrev(photos.get(photos.size() - 1 ));
//					}
//				}
//				if(photos.size() == 2){
//					if(page == 1){
//						container.setNext(photos.get(0));
//						container.setPhoto(photos.get(1));
//						container.setPrev(null);
//					}
//					if(page == 2){
//						container.setNext(null);
//						container.setPhoto(photos.get(0));
//						container.setPrev(photos.get(1));
//					}
//				}
//				if(photos.size() == 1){
//					container.setNext(null);
//					container.setPhoto(photos.get(0));
//					container.setPrev(null);
//					
//				}
				return container;
			}
		});
	}

}
